This code used to use the Scripting.Dictionary Object, which was originally listed in the registry as thread-safe. Using old versions of IIS, this code will operate, but under IIS 4.0 with the most recent updates, it would fail because the Dictionary object has been (correctly) listed as non-threadsafe in the registry. This causes an error in the global.asa when the the dictionary object is declared as an Application() variable. Currently, the application uses a SQL database to track users, which solves this problem.
It is very easy to track how many users are currently viewing your web site, including what page they are on, what IP address they are coming from, what browser they are using, and other useful information. In order to track the page they have accessed, you must include this piece of code at the top of every page that you wish to track:
'Update Current Users Page
set hdrconntemp=server.createobject("adodb.connection")
hdrconntemp.open Session("aspdb_ConnectionString")
hdrsqlString = "UPDATE UserTable SET currentpage = '" & Request.ServerVariables("SCRIPT_NAME") & "' WHERE session_id = " & Session.SessionID
Set hdrlogRS=hdrconntemp.execute(hdrsqlstring)
Set hdrlogRS=nothing
Set hdrconntemp = Nothing
In order to track the other variables, you need to make this database call in your global.asa:
In Application_OnStart:
Set Conn = Server.CreateObject("ADODB.Connection")
'Set dsn, dbuser, dbpass to your variables
sql = "DELETE FROM UserTable"
Conn.Execute sql
Set Conn = Nothing
' Track which IP's are currently connected
conntemp.Open Session("aspdb_ConnectionString")
sqlString = "INSERT INTO UserTable (session_id, ip, browser, created) " & _
" VALUES (" & Session.SessionID & ",'" & Request.ServerVariables("REMOTE_HOST") & "','" & Request.ServerVariables("HTTP_USER_AGENT") & "','" & now & "')"
logRS.Open sqlString, conntemp
Set logRS = Nothing
Set conntemp = Nothing
'Remove user from UserTable
set conntemp=server.createobject("adodb.connection")
set logRS = Server.CreateObject("ADODB.RecordSet")
conntemp.Open Session("aspdb_ConnectionString")
sqlString = "DELETE FROM UserTable WHERE session_id = " & Session.SessionID
logRS.Open sqlString, conntemp
Set logRS = Nothing
Set conntemp = Nothing
Finally, in order to display the information, you will need to create your tracking page, which I have called whoson.asp, because it is loosely based on an application of that name found in Active Server Pages Unleashed. Here is the source code for whoson.asp(note that I call my database table currentuser, and that I use a function called DBQuery for my database call):
1 <% OPTION EXPLICIT %>